/**
 *  This file is part of MunchLevel.
 *
 *  MunchLevel is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  MunchLevel is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with MunchLevel.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.munchlevel.data;

import java.io.Serializable;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.StringTokenizer;

import android.graphics.Bitmap;

import com.munchlevel.MunchLevelApplication;
import com.munchlevel.util.LimitedList;

public class PlayerInfo implements Serializable {

	private static final long serialVersionUID = 4064204154523482136L;

	public static final String NAME = "name";

	private int index = -1;

	private String name;

	private LimitedList<String> races = new LimitedList<String>(1);
	private LimitedList<String> classes = new LimitedList<String>(1);

	private boolean superMunchkin = false;
	private boolean halfblood = false;

	private boolean male = true;

	private int level = 1;

	private int gear = 0;

	private int levelGain = 0;
	private int levelLoss = 0;

	private int gearGain = 0;
	private int gearLoss = 0;

	private int bonusGain = 0;
	private int bonusLoss = 0;

	private Bitmap profile;

	public PlayerInfo() {
		races.add(MunchkinConfiguration.getNoRace());
		classes.add(MunchkinConfiguration.getNoClass());
	}

	public int getIndex() {
		return index;
	}

	public void setIndex(int index) {
		this.index = index;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public boolean isMale() {
		return male;
	}

	public void setMale(boolean male) {
		this.male = male;
	}

	public int getLevel() {
		return level;
	}

	public void addLevel(int level) {
		setLevel(getLevel() + level);
	}

	public void setLevel(int level) {

		if (this.level > level) {
			levelLoss += this.level - level;
		} else {
			levelGain += level - this.level;
		}

		this.level = level;
	}

	public int getGear() {
		return gear;
	}

	public void setGear(int gear) {

		if (this.gear > gear) {
			gearLoss += this.gear - gear;
		} else {
			gearGain += gear - this.gear;
		}

		this.gear = gear;
	}

	public List<String> getRace() {
		if (races.isEmpty())
			races.add(MunchkinConfiguration.getNoClass());
		return races;
	}

	public void addRace(String race) {
		if (!races.contains(race)) {
			races.add(race);
		}
	}

	public void addRace(Collection<String> races) {
		for (String race : races) {
			addRace(race);
		}
	}

	public void removeRace(String race) {
		races.remove(race);
		if (races.isEmpty())
			races.add(MunchkinConfiguration.getNoClass());
	}

	public List<String> getClazz() {
		if (classes.isEmpty())
			classes.add(MunchkinConfiguration.getNoClass());
		return classes;
	}

	public boolean hasClazz(int resourceId) {
		return hasClazz(MunchLevelApplication.getInstance().getResources().getString(resourceId));
	}

	public boolean hasClazz(String clazz) {
		return classes.contains(clazz);
	}

	public void addClazz(String clazz) {
		if (!classes.contains(clazz))
			classes.add(clazz);
	}

	public void removeClazz(String clazz) {
		classes.remove(clazz);
		if (classes.isEmpty())
			classes.add(MunchkinConfiguration.getNoClass());
	}

	public void addClazz(Collection<String> clazzes) {
		for (String clazz : clazzes) {
			addClazz(clazz);
		}
	}

	public boolean isSuperMunchkin() {
		return superMunchkin;
	}

	public void setSuperMunchkin(boolean superMunchkin) {
		this.superMunchkin = superMunchkin;
		if (superMunchkin)
			classes.setCapacity(2);
		else
			classes.setCapacity(1);
	}

	public boolean isHalfblood() {
		return halfblood;
	}

	public void setHalfblood(boolean halfblood) {
		this.halfblood = halfblood;

		if (halfblood)
			races.setCapacity(2);
		else
			races.setCapacity(1);
	}

	public int getLevelGain() {
		return levelGain;
	}

	public int getLevelLoss() {
		return levelLoss;
	}

	public int getGearGain() {
		return gearGain;
	}

	public int getGearLoss() {
		return gearLoss;
	}

	public int getBonusGain() {
		return bonusGain;
	}

	public void setBonusGain(int bonusGain) {
		this.bonusGain = bonusGain;
	}

	public void addBonusGain(int bonusGain) {
		this.bonusGain += bonusGain;
	}

	public int getBonusLoss() {
		return bonusLoss;
	}

	public void setBonusLoss(int bonusLoss) {
		this.bonusLoss = bonusLoss;
	}

	public void addBonusLoss(int bonusLoss) {
		this.bonusLoss += bonusLoss;
	}

	public void reset() {
		setLevel(1);
		setGear(0);
	}

	public Bitmap getProfile() {
		return profile;
	}

	public void setProfile(Bitmap profile) {
		this.profile = profile;
	}

	public static Comparator<PlayerInfo> LEVEL_COMPARATOR = new Comparator<PlayerInfo>() {

		@Override
		public int compare(PlayerInfo o1, PlayerInfo o2) {
			return (o2.getLevel() - o1.getLevel()) * 1000 + o2.getGear() - o1.getGear();
		}
	};

	public String toString() {
		return getName() + "#" + getIndex() + "#" + isMale() + "#" + getLevel() + "#" + getGear();
	}

	public static PlayerInfo parse(String s) {
		PlayerInfo info = new PlayerInfo();

		StringTokenizer st = new StringTokenizer(s, "#");

		info.setName(st.nextToken());
		info.setIndex(Integer.parseInt(st.nextToken()));
		info.setMale(Boolean.parseBoolean(st.nextToken()));
		info.setLevel(Integer.parseInt(st.nextToken()));
		info.setGear(Integer.parseInt(st.nextToken()));

		return info;
	}

}
